import ServerCenter from '../../Server/ServerCenter';
import PromptManage from '../PromptManage';
import GameUtil from '../../Fantasy/Util/GameUtil';


const {ccclass, property} = cc._decorator;

@ccclass
export default class LevelInfoCard extends cc.Component {

    @property(cc.Node)
    easy: cc.Node = null;
    @property(cc.Node)
    middle: cc.Node = null;
    @property(cc.Node)
    high: cc.Node = null;
    @property(cc.Node)
    lockPage: cc.Node = null;

    @property(cc.Label)
    redCrystal : cc.Label = null;
    @property(cc.Label)
    yellowCrystal : cc.Label = null;
    @property(cc.Label)
    purpleCrystal : cc.Label = null;
    @property(cc.Label)
    title : cc.Label = null;


    // LIFE-CYCLE CALLBACKS:

    currUnit = 0;
    bookId = 0;
    setLevelCard(bookId,currUnit,isLock){
        this.bookId = bookId;
        this.currUnit = currUnit;
        this.title.getComponent(cc.Label).string = this.setTitle(this.currUnit);
        if (isLock){
            this.lockPage.active = true;
            return;
        }
        else{
            this.lockPage.active = false;
        }
        let passInfo = ServerCenter.getUnitPassInfo(bookId,currUnit);
        cc.find('PASS',this.easy).active = passInfo.easy;
        cc.find('PASS',this.middle).active = passInfo.middle;
        cc.find('PASS',this.high).active = passInfo.high;

        let red = 0,yellow = 0,purple = 0;
        if (passInfo.easy){
            red += 5;
        }
        if (passInfo.middle){
            red += 10;
            yellow += 5;
        }
        if (passInfo.high){
            red += 20;
            yellow += 10;
            purple += 5;
        }
        this.redCrystal.string = red + '/35';
        this.yellowCrystal.string = yellow + '/15';
        this.purpleCrystal.string = purple + '/5';
    }
    setTitle(currUnit){
        var partId = currUnit % 2;
        let firsName = ServerCenter.getBookInfo(this.bookId,currUnit).unitName+"";
        if(partId == 0){
            var partName = "  partB"
        }else{
            var partName = "  partA";
        }
        let titleName = firsName + partName;
        // Log("第几单元：",currUnit,"-------部分",partId,"mingzi ",titleName)
        return titleName;
    }

    // update (dt) {}
    btnCallback(target,data){
        let part = 0;
        if (data == 'easy'){
            // this.loadNetSource();
            PromptManage.addRushLevelPop(data,this.bookId,this.currUnit);
            part = 1;
        }
        else if (data == 'middle'){
            PromptManage.addRushLevelPop(data,this.bookId,this.currUnit);
            part = 2;

        }
        else if (data == 'high'){
            PromptManage.addRushLevelPop(data,this.bookId,this.currUnit);
            part = 3;

        }else if(data == "lock"){
            let str = "通过上一关的每一个小关卡，本关才能解锁哦！";
            PromptManage.addToast(str);
        }
    }
    currUnitToBookUnit(){
        return Math.ceil(this.currUnit/2);
    }
    loadNetSource(){
        let info = ServerCenter.getBookInfo(this.bookId,this.currUnitToBookUnit());
        let worldList = info.wordsList;
        let srcArray = [];
        for(let item of worldList){
            let img = item.wordsImg;
            let mp3 = item.wordsMp3;
            srcArray.push(img);
            srcArray.push(mp3);
        }
        Log('需要远程加载的资源:',srcArray);
        let onProgress = (completedCount, totalCount, item)=>{
            let str = Math.floor(completedCount/totalCount * 100) + "%";
            Log('加载进度:',str,' | ',completedCount,'/',totalCount);
        }
        let complete = ()=>{
            Log('加载完成!');
        }
        cc.loader.load(srcArray,onProgress,complete);
    }
}
